﻿<template>
	<div class="mrsProduct-container">
		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品名称" prop="productName">
							<el-input v-model="state.ruleForm.productName" placeholder="请输入产品名称" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="展示价格" prop="price">
							<el-input v-model="state.ruleForm.price" placeholder="请输入展示价格" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实际价格" prop="cost">
							<el-input v-model="state.ruleForm.cost" placeholder="请输入实际价格" class="w100" clearable />
						</el-form-item>
					</el-col>


					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="时长类型" prop="timeType">
							<el-select v-model.trim="state.ruleForm.timeType" placeholder="请选择" class="w100">
								<el-option label="天" :value="1"></el-option>
								<el-option label="周" :value="2"></el-option>
								<el-option label="月" :value="3"></el-option>
								<el-option label="年" :value="4"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="state.ruleForm.status" class="ml-4">
								<el-radio :label="1">启用</el-radio>
								<el-radio :label="2">停用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="JumpUrl" prop="jumpUrl">
							<el-input v-model="state.ruleForm.jumpUrl" placeholder="请输入JumpUrl" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="IconUrl" prop="iconUrl">
							<el-input v-model="state.ruleForm.iconUrl" placeholder="请输入IconUrl" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="editMrsProduct" >
import { reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormRules } from "element-plus";
import mittBus from '/@/utils/mitt';

import { MrsProduct } from '/@/api/models';
import SysMrsProductApi from '/@/api/memberRights/product';

// 定义变量内容
const props = defineProps({
	title: String,
});
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as MrsProduct,
});

// 删除非必填规则
const rules = ref<FormRules>({
	productName: [{ required: true, message: '请输入产品名称！', trigger: 'blur', },],
	price: [{ required: true, message: '请输入展示价格！', trigger: 'blur', },],
	cost: [{ required: true, message: '请输入实际价格！', trigger: 'blur', },],
	iconUrl: [{ required: true, message: '请输入IconUrl！', trigger: 'blur', },],
	jumpUrl: [{ required: true, message: '请输入JumpUrl！', trigger: 'blur', },],
	timeType: [{ required: true, message: '请输入时长类型 1:天 2:周 3:月 4:年！', trigger: 'blur', },],
	status: [{ required: true, message: '请输入状态 0:未知 1:启用 2:停用！', trigger: 'blur', },],
	remark: [{ required: true, message: '请输入备注！', trigger: 'blur', },],
});

// 打开弹窗
const openDialog = (row: MrsProduct) => {
	state.ruleForm = row;
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	mittBus.emit('submitRefresh');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean, fields?: any) => {
		if (valid) {
			let res;
			if (state.ruleForm.id != undefined && state.ruleForm.id > 0)
				res = await SysMrsProductApi().editPost(state.ruleForm);
			else
				res = await SysMrsProductApi().addPost(state.ruleForm);
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else {
				ElMessage.success("操作成功");
				closeDialog();
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

// 导出对象 将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>